package dao.mysql;

import dao.DAOException;
import dao.ISubjectDAO;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import modelInterfaces.AbstractModelFactory;
import modelInterfaces.IEventType;
import modelInterfaces.ISubject;

/**
 *
 * @author salvador
 */
public class SubjectMySqlDAO implements ISubjectDAO {

	private String PROPERTIES_FILE_PATH = "config/MySqlDAO.props";
	
    private String EVENT_TABLE_NAME = "event";
    private String EVENT_SUBJECT_COLUMN_NAME = "subject";
    private String EVENT_TYPE_COLUMN_NAME = "eventType";
	
	public SubjectMySqlDAO()
	{
		Properties prop = new Properties();
		try {
			prop.load(new FileInputStream(PROPERTIES_FILE_PATH));
			EVENT_TABLE_NAME = 			prop.getProperty("EVENT_TABLE_NAME") != null ? 				prop.getProperty("EVENT_TABLE_NAME") :				this.EVENT_TABLE_NAME;
			EVENT_SUBJECT_COLUMN_NAME = prop.getProperty("EVENT_SUBJECT_COLUMN_NAME") != null ? 	prop.getProperty("EVENT_SUBJECT_COLUMN_NAME") :		this.EVENT_SUBJECT_COLUMN_NAME;
	        EVENT_TYPE_COLUMN_NAME = 	prop.getProperty("EVENT_EVENT_TYPE_COLUMN_NAME") != null ? 	prop.getProperty("EVENT_EVENT_TYPE_COLUMN_NAME") :	this.EVENT_TYPE_COLUMN_NAME;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
    
    @Override
	public List<ISubject> retrieveAll() throws DAOException {
		String query = "SELECT "+ EVENT_SUBJECT_COLUMN_NAME +" FROM "+ EVENT_TABLE_NAME +" GROUP BY "+ EVENT_SUBJECT_COLUMN_NAME +" ORDER BY "+ EVENT_SUBJECT_COLUMN_NAME;
    	ResultSet rs = MySqlDBConnection.getInstance().executeQuery(query);
        List<ISubject> subjectList = new ArrayList<ISubject>();
        try
        {
            while (rs.next())
            {
            	ISubject subject = AbstractModelFactory.getModelFactory().getUser();
                subject.setId(rs.getInt(EVENT_SUBJECT_COLUMN_NAME));
                subjectList.add(subject);
            }
        }
        catch (SQLException ex)
        {
            throw new DAOException(ex.getErrorCode(), ex.getMessage());
        }
        return subjectList;
    }

	@Override
	public List<ISubject> retrieveAllExcept(ISubject subject) throws DAOException {
		String query = "SELECT "+ EVENT_SUBJECT_COLUMN_NAME +" FROM "+ EVENT_TABLE_NAME +" where "+ EVENT_SUBJECT_COLUMN_NAME +" != '"+subject.getId()+"' GROUP BY "+ EVENT_SUBJECT_COLUMN_NAME +" ORDER BY "+ EVENT_SUBJECT_COLUMN_NAME;
		ResultSet rs = MySqlDBConnection.getInstance().executeQuery(query);
        List<ISubject> distinctSubjectList = new ArrayList<ISubject>();
        try
        {
            while (rs.next())
            {
            	ISubject distinctSubject = AbstractModelFactory.getModelFactory().getUser();
                distinctSubject.setId(rs.getInt(EVENT_SUBJECT_COLUMN_NAME));
                distinctSubjectList.add(distinctSubject);
            }
        }
        catch (SQLException ex)
        {
            throw new DAOException(ex.getErrorCode(), ex.getMessage());
        }
        return distinctSubjectList;
	}
	
	@Override
	public List<ISubject> retrieveAllFromType(int type) throws DAOException {
		String query = "SELECT "+ EVENT_SUBJECT_COLUMN_NAME +" FROM "+ EVENT_TABLE_NAME +" where "+ EVENT_TYPE_COLUMN_NAME +" = "+ type + " GROUP BY "+ EVENT_SUBJECT_COLUMN_NAME +" ORDER BY "+ EVENT_SUBJECT_COLUMN_NAME;
		ResultSet rs = MySqlDBConnection.getInstance().executeQuery(query);
        List<ISubject> subjectList = new ArrayList<ISubject>();
        try
        {
            while (rs.next())
            {
            	ISubject subject = AbstractModelFactory.getModelFactory().getUser();
                subject.setId(rs.getInt(EVENT_SUBJECT_COLUMN_NAME));
                subjectList.add(subject);
            }
        }
        catch (SQLException ex)
        {
            throw new DAOException(ex.getErrorCode(), ex.getMessage());
        }
        return subjectList;
	}
	
	@Override
	public void create(ISubject user) throws DAOException {
		// TODO [programador] Auto-generated method stub
		
	}

	@Override
	public void delete(ISubject user) throws DAOException {
		// TODO [programador] Auto-generated method stub
		
	}

	@Override
	public void update(ISubject user) throws DAOException {
		// TODO [programador] Auto-generated method stub
		
	}
	
}
